package cap.dao.impl;


import cap.bean.Counter;
import cap.dao.CounterDao;
import cap.db.DBPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CounterDaoImpl implements CounterDao {
	Connection conn=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	/**
	 *获取网站浏览数
	 */
	@Override
	public Counter getCounter() {
		Counter cnt = null;
		try {
			String sql="select * from counter where id=1";
			cnt = new Counter();
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if (rs.next()) {
				cnt.setCount(rs.getInt("num"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs,ps,conn);
		}
		return cnt;
	}
	/**
	 *设置浏览数
	 */
	@Override
	public int setNum(int num) {
		int res = -1;
		try {
			String sql="update counter set num=? where id=1";
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			ps.setInt(1,num);
			res=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs,ps,conn);
		}
		return res;
	}
}
